package com.testplatfrom.project.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.testplatfrom.common.core.exception.project.ProjectException;
import com.testplatfrom.common.security.utils.SecurityUtils;
import com.testplatfrom.project.domain.TestplatfromAssert;
import com.testplatfrom.project.domain.TestplatfromVariable;
import com.testplatfrom.project.enumproject.variable.ScenarioEnum;
import com.testplatfrom.project.enumproject.variable.VarableApiEnums;
import com.testplatfrom.project.enumproject.variable.VarableUiEnums;
import com.testplatfrom.project.mapper.TestplatfromVariableMapper;
import com.testplatfrom.project.service.TestplatfromVariableService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 杨森
 * @since 2023-08-29
 */
@Service
public class TestplatfromVariableServiceImpl extends ServiceImpl<TestplatfromVariableMapper, TestplatfromVariable> implements TestplatfromVariableService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addOrUpdateTestplatfromVariableList(List<TestplatfromVariable> testplatfromVariableList,String environmentId,Boolean bool) {
        for (TestplatfromVariable testplatfromVariable : testplatfromVariableList) {
            testplatfromVariable.setCreateBy(SecurityUtils.getUsername());
            testplatfromVariable.setCreateTime(LocalDateTime.now());
            ScenarioEnum anEnum = ScenarioEnum.getByKey(testplatfromVariable.getApplicationScenario());
            if (ObjectUtil.isNull(anEnum)){
                throw new ProjectException("应用场景不存在");
            }
            testplatfromVariable.setApplicationScenario(anEnum.name());
            //变量类型 todo
//            ScenarioEnum anEnum = ScenarioEnum.getByKey(testplatfromVariable.getApplicationScenario());
            if (ObjectUtil.isNull(anEnum)){
                throw new ProjectException("应用场景类型错误");
            }
            testplatfromVariable.setApplicationScenario(anEnum.name());
            //接口测试
            if (anEnum.name().equals("API")){
                VarableApiEnums apiEnums = VarableApiEnums.getByKey(testplatfromVariable.getVariableType());
                if (ObjectUtil.isNull(apiEnums)){
                    throw new ProjectException("接口测试的变量类型错误");
                }
                testplatfromVariable.setVariableType(apiEnums.name());
            }
            //UI
            if (anEnum.name().equals("UI")){
                VarableUiEnums apiEnums = VarableUiEnums.getByKey(testplatfromVariable.getVariableType());
                if (ObjectUtil.isNull(apiEnums)){
                    throw new ProjectException("UI测试的变量类型错误");
                }
                testplatfromVariable.setVariableType(apiEnums.name());
            }

        }

        if (!bool){
            LambdaQueryWrapper<TestplatfromVariable> testplatfromHttpconfigurationLambdaQueryWrapper=new LambdaQueryWrapper<>();
            testplatfromHttpconfigurationLambdaQueryWrapper.eq(TestplatfromVariable::getEnvironmentId, environmentId);
            this.remove(testplatfromHttpconfigurationLambdaQueryWrapper);
        }
        return saveBatch(testplatfromVariableList);
    }

    @Override
    public List<TestplatfromVariable> getTestplatfromVariableList(String environmentId) {
        List<TestplatfromVariable> testplatfromVariableList = this.list(new LambdaQueryWrapper<TestplatfromVariable>().eq(TestplatfromVariable::getEnvironmentId, environmentId));
        if (CollectionUtils.isEmpty(testplatfromVariableList)){
            return null;
        }
        for (TestplatfromVariable testplatfromVariable : testplatfromVariableList) {
            ScenarioEnum scenarioEnum = ScenarioEnum.valueOf(testplatfromVariable.getApplicationScenario());
            testplatfromVariable.setApplicationScenario(scenarioEnum.getMessage());

            if (scenarioEnum.name().equals("API")){
                VarableApiEnums varableApiEnums = VarableApiEnums.valueOf(testplatfromVariable.getVariableType());
                testplatfromVariable.setVariableType(varableApiEnums.getMessage());
            }
            //UI
            if (scenarioEnum.name().equals("UI")){
                VarableUiEnums apiEnums = VarableUiEnums.valueOf(testplatfromVariable.getVariableType());
                testplatfromVariable.setVariableType(apiEnums.getMessage());
            }
        }
        return testplatfromVariableList;
    }
}
